package com.hxzy.tms.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.hxzy.tms.Assert;
import com.hxzy.tms.ExtendsActionSupport;
import com.hxzy.tms.SessionHolder;
import com.hxzy.tms.action.vo.TeamVo;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TeamService;
import com.opensymphony.xwork2.ActionContext;

public class TeamAction extends ExtendsActionSupport {
    private static final long serialVersionUID = 2581338981313142112L;
    public static final String TEAM_KEY = "team";
    private TeamVo team;
    List<Team> teams;
    private TeamService teamService;
    private String title;// 标题
    private String email;// 邮箱
    private String indexKey = null;
    //团队的首页
    public String index() throws Exception {
        Assert.notEmpty(indexKey, "丢失参数");
        Team t = null;
        try { 
            t = teamService.read(new Long(indexKey)).data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(t == null){
            t = teamService.read(indexKey).data;
        }
        if (t == null) {
            addActionError("团队不存在");
            return ERROR;
        }
        Map<String, Object> values = new HashMap<String, Object>();
        values.put("team", t);
//        values.put("members", teamService.members(t.getId()).data);
//        values.put("projects", teamService.projects(t.getId()).data);
//        values.put("tasks", (new Gson()).toJson(taskService.list(t.getId(), null).data));
//        values.put("today", new Date());
        ActionContext.getContext().getValueStack().push(values);
        return "index";
    }

    /**
     * 
     * @return
     * @throws Exception
     */
    public String detail() throws Exception {
        if (team != null && team.getId() != null) {
            Team t = teamService.read(team.getId()).data;
            this.team.setName(t.getName());
            this.team.setDesc(t.getDesc());
            this.team.setSubdomain(t.getSubdomain());
            this.team.setStatus(t.getStatus());
            this.team.setIntro(t.getIntro());
            this.team.setStartDate(new Date(t.getGmtBegin() * 1000));
            this.team.setEndDate(new Date(t.getGmtEnd() * 1000));
        }
        return INPUT;
    }

    public String post() throws Exception {
        title = "创建新团队";

        ServiceResult<Team> result = null;
        if (team.getId() != null) {
            result = teamService.update(team.getId(), team.getName(), team.getDesc(), team.getIntro(),
                    team.getSubdomain(), team.getStartDate(), team.getEndDate(), team.getStatus());
        } else {
            result = teamService.create(team.getName(), team.getDesc(), team.getIntro(), team.getSubdomain(),
                    team.getStartDate(), team.getEndDate(), team.getStatus());
        }
        if (result.status != 1) {
            switch (result.status) {
            case -1:
                addFieldError("team.startDate", result.message);
                break;
            case -2:
                addFieldError("team.subdomain", result.message);
                break;
            case -3:
                addFieldError("team.name", result.message);
                break;
            case -4:
                addFieldError("team.subdomain", result.message);
                break;
            case -5:
                addFieldError("team.name", result.message);
                break;
            }
            return INPUT;
        }
        if (result.status == 1) {
            addActionMessage("保存成功");
        }
        this.team.setId(result.data.getId());
        return SUCCESS;
    }

    
    public String invite() throws Exception {
        title = "邀请成员";

        if (isGet()) {
            if (team == null || team.getId() == null) {
                addActionError("丢失团队 id 参数");
                return ERROR;
            }
        }

        if (isPost()) {
            if (StringUtils.isBlank(email)) {
                addFieldError("email", "请输入想要邀请用户的邮箱地址");
                return INPUT;
            }
            int result = teamService.invite(SessionHolder.get().getId(), team.getId(), email);
            switch (result) {
            case -1:
                addFieldError("email", "您不是团队创建者，不能发起邀请");
                break;
            case -2:
                addFieldError("email", "email 对应的用户不存在");
                break;
            case -3:
                addFieldError("email", "该用户已存在组内了");
                break;
            case -4:
                addFieldError("email", "不能邀请自己，您是团队的创建者");
                break;
            case 1:
                addActionMessage("邀请成功");
                return SUCCESS;
            }
        }
        return INPUT;
    }
    
    public String list() throws Exception {

        return "list";
    }

    public TeamVo getTeam() {
        return team;
    }

    public void setTeam(TeamVo team) {
        this.team = team;
    }

    public List<Team> getTeams() {
        return teams;
    }

    public void setTeams(List<Team> teams) {
        this.teams = teams;
    }

    public TeamService getTeamService() {
        return teamService;
    }

    public void setTeamService(TeamService teamService) {
        this.teamService = teamService;
    }

    public String getTitle() {
        if (team != null && team.getId() != null && team.getName() != null) {
            return String.format("编辑%s团队的数据", team.getName());
        }
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getIndexKey() {
        return indexKey;
    }

    public void setIndexKey(String indexKey) {
        this.indexKey = indexKey;
    }

}
